﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;

namespace IHISCETeim.DAL
{
    public class VIPAgentExchangeDAL
    {
        /// <summary>
        /// 大于表示成功
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="listDetail"></param>
        /// <returns></returns>
        public static int Add(VIPAgentExchangeInfo obj,List<VIPAgentExchangeDetailInfo> listDetail)
        {

            Database DB = DatabaseFactory.CreateDatabase();
            using (DbConnection conn = DB.CreateConnection())
            {
                //打开连接
                conn.Open();
                //创建事务
                DbTransaction trans = conn.BeginTransaction();
                try
                {

                    DbCommand dbCommand = DB.GetStoredProcCommand("VIPAgentExchange_Add");
                    DB.AddInParameter(dbCommand, "DocNum", DbType.String, obj.DocNum);
                    DB.AddInParameter(dbCommand, "ExChangeDate", DbType.DateTime, obj.ExChangeDate);
                    DB.AddInParameter(dbCommand, "UserId", DbType.Int32, obj.UserId);
                    DB.AddInParameter(dbCommand, "AgentName", DbType.String, obj.AgentName);
                    DB.AddInParameter(dbCommand, "RealName", DbType.String, obj.RealName);
                    DB.AddInParameter(dbCommand, "Tel", DbType.String, obj.Tel);
                    DB.AddInParameter(dbCommand, "Address", DbType.String, obj.Address);
                    DB.AddInParameter(dbCommand, "TotalPoint", DbType.Int32, obj.TotalPoint);
                    DB.AddInParameter(dbCommand, "TotalMoney", DbType.Decimal, obj.TotalMoney);
                    DB.AddInParameter(dbCommand, "Remark", DbType.String, obj.Remark);

                    //return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
                    int AgentExID=  Convert.ToInt32(DB.ExecuteScalar(dbCommand, trans));

                    foreach (VIPAgentExchangeDetailInfo itm in listDetail)
                    {
                        DbCommand dbCommandDetail = DB.GetStoredProcCommand("VIPAgentExchange_AddDetail");
                        DB.AddInParameter(dbCommandDetail, "AgentExID", DbType.Int32, AgentExID);
                        DB.AddInParameter(dbCommandDetail, "ItemCode", DbType.String, itm.ItemCode);
                        DB.AddInParameter(dbCommandDetail, "ItemName", DbType.String, itm.ItemName);
                        DB.AddInParameter(dbCommandDetail, "Quantity", DbType.Decimal, itm.Quantity);
                        DB.AddInParameter(dbCommandDetail, "Price", DbType.Decimal, itm.Price);
                        DB.AddInParameter(dbCommandDetail, "LineTotal", DbType.Decimal, itm.LineTotal);
                        DB.AddInParameter(dbCommandDetail, "LinePoint", DbType.Int32, itm.LinePoint);

                        //return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
                        DB.ExecuteScalar(dbCommandDetail,trans);
                    }


                    trans.Commit();
                    return AgentExID;

                }
                catch (Exception exc)
                {
                    SEMailListDAL.AddAppError(string.Format("类方法:{0}<BR/> 错误:{1}", "VIPAgentExchangeDAL.Add", exc.Message));
                    //发生异常，事务回滚
                    trans.Rollback();
                    return 0;
                }
                finally
                {
                    //关闭连接
                    conn.Close();
                }

            }

        }
        
        public static bool Delete(int AgentExID)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            using (DbConnection conn = DB.CreateConnection())
            {
                //打开连接
                conn.Open();
                //创建事务
                DbTransaction trans = conn.BeginTransaction();
                try
                {

                    DbCommand dbCommand = DB.GetStoredProcCommand("VIPAgentExchange_Delete", AgentExID);
            
                    DB.ExecuteScalar(dbCommand,trans);
                    trans.Commit();
                    return true;
                }
                catch (Exception exc)
                {
                    SEMailListDAL.AddAppError(string.Format("类方法:{0}<BR/>.ID:{2}<BR/> 错误:{1}", "VIPAgentExchangeDAL.Delete", exc.Message, AgentExID));
                    //发生异常，事务回滚
                    trans.Rollback();
                    return false;
                }
                finally
                {
                    //关闭连接
                    conn.Close();
                }
            }
            
        }
       
        public static IDataReader GetAllDetail(int AgentExID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VIPAgentExchange_GetAllDetail", AgentExID);
            return DB.ExecuteReader(dbCommand);
        }
        public static IDataReader GetList(int AgentExID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VIPAgentExchange_GetList", AgentExID);
            return DB.ExecuteReader(dbCommand);
        }
        public static DataTable GetPagination(string WhereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VIPAgentExchange_GetPagination");
            DB.AddInParameter(dbCommand, "WhereSql", DbType.String, WhereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String, Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32, 8);
            DataTable dt = DB.ExecuteDataSet(dbCommand).Tables[0];

            TotalCount = Convert.ToInt32(DB.GetParameterValue(dbCommand, "TotalCount"));
            return dt;
        }
        public static int UpdateAudit(VIPAgentExchangeInfo obj, List<VIPAgentExchangeDetailInfo> listDetail)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            using (DbConnection conn = DB.CreateConnection())
            {
                //打开连接
                conn.Open();
                //创建事务
                DbTransaction trans = conn.BeginTransaction();
                try
                {

                    DbCommand dbCommand = DB.GetStoredProcCommand("VIPAgentExchange_UpdateAudit");
                    DB.AddInParameter(dbCommand, "AgentExID", DbType.Int32, obj.AgentExID);
                    DB.AddInParameter(dbCommand, "RealName", DbType.String, obj.RealName);
                    DB.AddInParameter(dbCommand, "Tel", DbType.String, obj.Tel);
                    DB.AddInParameter(dbCommand, "Address", DbType.String, obj.Address);
                    DB.AddInParameter(dbCommand, "TotalPoint", DbType.Int32, obj.TotalPoint);
                    DB.AddInParameter(dbCommand, "TotalMoney", DbType.Decimal, obj.TotalMoney);
                    DB.AddInParameter(dbCommand, "Remark", DbType.String, obj.Remark);            
                    DB.ExecuteScalar(dbCommand,trans);

                    DbCommand dbCommandDel = DB.GetStoredProcCommand("VIPAgentExchange_DeleteDetail", obj.AgentExID);
                    DB.ExecuteScalar(dbCommandDel,trans);

                    foreach (VIPAgentExchangeDetailInfo itm in listDetail)
                    {
                        DbCommand dbCommandDetail = DB.GetStoredProcCommand("VIPAgentExchange_AddDetail");
                        DB.AddInParameter(dbCommandDetail, "AgentExID", DbType.Int32, obj.AgentExID);
                        DB.AddInParameter(dbCommandDetail, "ItemCode", DbType.String, itm.ItemCode);
                        DB.AddInParameter(dbCommandDetail, "ItemName", DbType.String, itm.ItemName);
                        DB.AddInParameter(dbCommandDetail, "Quantity", DbType.Decimal, itm.Quantity);
                        DB.AddInParameter(dbCommandDetail, "Price", DbType.Decimal, itm.Price);
                        DB.AddInParameter(dbCommandDetail, "LineTotal", DbType.Decimal, itm.LineTotal);
                        DB.AddInParameter(dbCommandDetail, "LinePoint", DbType.Int32, itm.LinePoint);

                        //return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
                        DB.ExecuteScalar(dbCommandDetail, trans);
                    }

                    trans.Commit();
                    return 1;

                }
                catch (Exception exc)
                {
                    SEMailListDAL.AddAppError(string.Format("类方法:{0}<BR/> 错误:{1}", "VIPAgentExchangeDAL.UpdateAudit", exc.Message));
                    //发生异常，事务回滚
                    trans.Rollback();
                    return 0;
                }
                finally
                {
                    //关闭连接
                    conn.Close();
                }

            }
        }
        public static bool UpdateReceipt(int AgentExID, DateTime ReceiptDate, string Remark)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VIPAgentExchange_UpdateReceipt");
            DB.AddInParameter(dbCommand, "AgentExID", DbType.Int32, AgentExID);
            DB.AddInParameter(dbCommand, "ReceiptDate", DbType.DateTime, ReceiptDate);
            DB.AddInParameter(dbCommand, "Remark", DbType.String, Remark);

            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
        public static bool UpdateShipped(int AgentExID, DateTime ShippedDate, string ExpressName, string ExpressDocNum, string RealName, string Tel, string Address, string Remark)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("VIPAgentExchange_UpdateShipped");
            DB.AddInParameter(dbCommand, "AgentExID", DbType.Int32, AgentExID);
            DB.AddInParameter(dbCommand, "ShippedDate", DbType.DateTime, ShippedDate);
            DB.AddInParameter(dbCommand, "ExpressName", DbType.String, ExpressName);
            DB.AddInParameter(dbCommand, "ExpressDocNum", DbType.String, ExpressDocNum);
            DB.AddInParameter(dbCommand, "RealName", DbType.String, RealName);
            DB.AddInParameter(dbCommand, "Tel", DbType.String, Tel);
            DB.AddInParameter(dbCommand, "Address", DbType.String, Address);
            DB.AddInParameter(dbCommand, "Remark", DbType.String, Remark);

            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
    }
}
